package eco2automotive.util;

import java.awt.Toolkit;
import java.util.Timer;
import java.util.TimerTask;
/**  
 *  Stop x second(s) all actions.
  * @version 1.0
  * @autor Fco. Javier Sánchez Esparza
  */ 

public class Clock {
  Toolkit toolkit;
  Timer timer;
  boolean flag=true;
  int sec;
  /**
   * Start the clock
   * @param seconds
   */
  public Clock(int seconds) {
    sec=seconds;
    toolkit = Toolkit.getDefaultToolkit();
    timer = new Timer();
    timer.schedule(new RemindTask(), seconds * 1000);
  }
 
  /**
   * Stop the timer
   */
  public void stopTimer() {
	if (timer!=null) {
	    timer.cancel();
	}
         }
  /**
   * Time´s up?
   * @return
   */
  public boolean getFlag(){return this.flag;}

  
  class RemindTask extends TimerTask {

    public void run() {
      System.out.println("***************Time's up!");
      //timer.cancel(); //Not necessary because we call System.exit    
      flag=false;
      try{Thread.sleep(5000);}catch(Exception ex){}//Wait for RX 
      try{
      timer.schedule(new RemindTask(), sec * 1000);
      flag=true;            
      }
      catch(Exception e){}
      
    //  System.exit(0); //Stops the AWT thread (and everything else)
    }
    
  }
   
}
